package cn.gson.springboot.model.service.presaleservice;

import cn.gson.springboot.model.vo.presalevo.Data;
import cn.gson.springboot.model.mapper.presalemapper.PsBusinessViewMapper;
import cn.gson.springboot.model.vo.presalevo.MonthInfo;
import cn.gson.springboot.model.vo.presalevo.TimeVo;
import cn.gson.springboot.model.vo.presalevo.TranslationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;

@Service
@Transactional
public class PsBusinessViewService {
    @Autowired
    PsBusinessViewMapper psBusinessViewMapper;
    List<TimeVo> one  = new ArrayList<>();//接收新增数
    List<TimeVo> two = new ArrayList<>();//接收赢单数
    /*商机的所有阶段*/
    public List<String> all_stage(){
        return psBusinessViewMapper.all_stage();
    }

    /*统计各阶段的商机数量*/
    public List<Data> business_stage_number (Integer empId,Integer deptId,String time){
        String [] date =new String[2];
        String startTime = "";
        String endTime = "";
        if(time != ""){
            date= time.split(",");//把时间用,分割
            startTime = date[0];
            endTime = date[1];
        }
        List<Data> data = psBusinessViewMapper.business_stage_number(empId,deptId,startTime,endTime);
        List<String> speedInfo = all_stage();//所有阶段
        List<Data> list = new ArrayList<>();//存储处理完的数据
        //查询出来的数据没有某个阶段 则处理，某个阶段的数量为0
        for(int i=0;i<speedInfo.size();i++){
            boolean flage=false;
            for (int k=0;k<data.size();k++){
                if(speedInfo.get(i).equals(data.get(k).getName())){
                    Data d = new Data();
                    d.setName(data.get(k).getName());
                    d.setValue(data.get(k).getValue());
                    list.add(d);
                    flage = true;
                    break;
                }
            }
            if(!flage){
                Data da = new Data();
                da.setName(speedInfo.get(i));
                da.setValue(0);
                list.add(da);
            }

        }
        return list;
    }



    //计算商机阶段的转化率 各阶段的数量
    public List<TranslationVo> translation(Integer empId,Integer deptId,String time){
        String [] date =new String[2];
        String startTime = "";
        String endTime = "";
        if(time != ""){
            date= time.split(",");//把时间用,分割
            startTime = date[0];
            endTime = date[1];
        }
        List<String> speedInfo = all_stage();//所有阶段
        List<TranslationVo> translation = psBusinessViewMapper.translation(empId,deptId,startTime,endTime);
        //查询出来的数据没有某个阶段 则处理，某个阶段的数量为0 预期金额为0
        List<TranslationVo> list = new ArrayList<>();
        for(int i=0;i<speedInfo.size();i++){
            boolean flage=false;
            for (int k=0;k<translation.size();k++){
                if(speedInfo.get(i).equals(translation.get(k).getSppedContext())){
                    TranslationVo t = new TranslationVo();
                    t.setSppedContext(translation.get(k).getSppedContext());
                    t.setNumber(translation.get(k).getNumber());
                    t.setAmount(translation.get(k).getAmount());
                    list.add(t);
                    flage = true;
                    break;
                }
            }
            if(!flage){
                TranslationVo t1 = new TranslationVo();
                t1.setSppedContext(speedInfo.get(i));
                t1.setNumber(0);
                t1.setAmount(new BigDecimal("0"));
                list.add(t1);

            }

        }

        for (int index =0;index<=list.size();index++){
            if(index+1<list.size()){
                DecimalFormat  df ;
                float num;
                int nextVlue=list.get(index+1).getNumber();
                int value=list.get(index).getNumber();
                if(nextVlue ==0 || value==0){
                    num = 0;
                }else{
                    num=(float) nextVlue*100/value;
                }
                df =new DecimalFormat("0.00");
                String s=df.format(num)+"%";
                list.get(index).setPcPercent(s);
            }
        }
        return list;

    }
    /**
     * 某年每月
     *  新增商机数和赢单数
     */
    public Map<String,Object> business_month_count(Integer empId,Integer deptId,Integer year){
        List<MonthInfo> month = getMonth(year);
        List<TimeVo> addNumber = psBusinessViewMapper.count_month_add_number(empId,deptId,year);//数据库查到的新增商机数
        List<TimeVo> addMonthNumber = new ArrayList<>();//每月的新增商机数

        if(addNumber.size()==0){//如果集合为空，则每个月的数量都为0
            for(int i=0;i<month.size();i++){
                TimeVo t = new TimeVo();
                t.setTime(month.get(i).getMonthValue());
                t.setNumber(0);
                addMonthNumber.add(t);
            }
        }else{
            for(int i=0;i<month.size();i++){
                boolean flage=false;
                for (int k=0;k<addNumber.size();k++){
                    if(month.get(i).getMonthValue().equals(addNumber.get(k).getTime())){
                        TimeVo time1 = new TimeVo();
                        time1.setTime(addNumber.get(k).getTime());
                        time1.setNumber(addNumber.get(k).getNumber());
                        addMonthNumber.add(time1);
                        flage=true;
                        break;
                    }
                }
                if(!flage){
                    TimeVo time = new TimeVo();
                    time.setTime(month.get(i).getMonthValue());
                    time.setNumber(0);
                    addMonthNumber.add(time);
                }
            }
        }
        List<TimeVo> winNumber = psBusinessViewMapper.count_month_win_number(empId,deptId,year);//数据库查到的赢单数
        List<TimeVo> winMonthNumber = new ArrayList<>();//每月的赢单数
        if(winNumber.size()==0){
            for (int a=0;a<month.size();a++){
                TimeVo t1= new TimeVo();
                t1.setTime(month.get(a).getMonthValue());
                t1.setNumber(0);
                winMonthNumber.add(t1);
            }
        }else{
            for(int b=0;b<month.size();b++){
                boolean flag=false;
                for(int c=0;c<winNumber.size();c++){
                    if(month.get(b).getMonthValue().equals(winNumber.get(c).getTime())){
                        TimeVo tt1= new TimeVo();
                        tt1.setTime(winNumber.get(c).getTime());
                        tt1.setNumber(winNumber.get(c).getNumber());
                        winMonthNumber.add(tt1);
                        flag=true;
                        break;
                    }
                    if(!flag){
                        TimeVo tt2= new TimeVo();
                        tt2.setTime(month.get(b).getMonthValue());
                        tt2.setNumber(0);
                        winMonthNumber.add(tt2);
                    }
                }
            }
        }
        if(one.size()>0){
            one.clear();
            two.clear();
        }
        one =  addMonthNumber;
        two = winMonthNumber;
        Map<String,Object> map = new HashMap<>();
        map.put("addMonthCount",addMonthNumber);
        map.put("winMonthNumber",winMonthNumber);
        return map;
    }
    /**
     * 某年某月每天
     * 新增商机数和赢单数
     */
    public Map<String,Object> business_day_count(Integer empId,Integer deptId,Integer year,Integer month){
        List<MonthInfo> list = getDay(year,month);
        List<TimeVo>  addDay = psBusinessViewMapper.count_day_add_number(empId,deptId,year,month);//数据库查询的天数
        List<TimeVo> addDayCount = new ArrayList<>();//处理新增商机数
        if(addDay.size()==0){//数据为空时
            for(int i=0;i<list.size();i++){
                TimeVo t = new TimeVo();
                t.setTime(list.get(i).getMonthValue());
                t.setNumber(0);
                addDayCount.add(t);
            }
        }else{
            for(int i=0;i<list.size();i++){
                boolean f = false;
                for(int j=0;j<addDay.size();j++){
                    if(list.get(i).getMonthValue().equals(addDay.get(j).getTime())){
                        TimeVo time1 = new TimeVo();
                        time1.setTime(addDay.get(j).getTime());
                        time1.setNumber(addDay.get(j).getNumber());
                        addDayCount.add(time1);
                        f = true;
                        break;
                    }
                }
                if(!f){
                    TimeVo time2 = new TimeVo();
                    time2.setTime(list.get(i).getMonthValue());
                    time2.setNumber(0);
                    addDayCount.add(time2);
                }
            }
        }
        List<TimeVo>  winDay = psBusinessViewMapper.count_day_win_number(empId,deptId,year,month);//数据库查询的天数
        List<TimeVo> winDayCount = new ArrayList<>();//处理赢单数
        if(winDay.size()==0){
            for(int i=0;i<list.size();i++){
                TimeVo time = new TimeVo();
                time.setTime(list.get(i).getMonthValue());
                time.setNumber(0);
                winDayCount.add(time);
            }
        }else{
            for(int i=0;i<list.size();i++){
                boolean flag2 = false;
                for(int k=0;k<winDay.size();k++){
                    if(list.get(i).getMonthValue().equals(winDay.get(k).getTime())){
                        TimeVo timeVo1= new TimeVo();
                        timeVo1.setTime(winDay.get(k).getTime());
                        timeVo1.setNumber(winDay.get(k).getNumber());
                        winDayCount.add(timeVo1);
                        flag2=true;
                        break;
                    }
                    if(!flag2){
                        TimeVo timeVo2= new TimeVo();
                        timeVo2.setTime(list.get(i).getMonthValue());
                        timeVo2.setNumber(0);
                        winDayCount.add(timeVo2);
                    }
                }
            }

        }
        if(one.size()>0){
            one.clear();
            two.clear();
        }
        one =  addDayCount;
        two = winDayCount;

        Map<String,Object> map = new HashMap<>();
        map.put("addDayCount",addDayCount);
        map.put("winDayCount",winDayCount);
        return map;
    }
    /**
     * 计算赢单率
     */
    public Map<String,Object> calculateWinningRate(Integer year,Integer month){
            String title = "";//标题
            List<String> header = new ArrayList<>();//表头
            List<String> addnum = new ArrayList<>();//新增数
            List<String> winnum = new ArrayList<>();//赢单数
            List<String> winprobability = new ArrayList<>();//赢单率
            if(month > 0){//每天
                title = month+""+"月";
            }else{//每月
                title = year+""+"年";
            }
            header.add(title);//添加标题
            addnum.add("新增数");//新增数
            winnum.add("赢单数");
            winprobability.add("赢单率");
            int addCount = 0;//总计新增数
            int winCount = 0;//总计赢单数
            for(int i = 0;i<one.size();i++){
                DecimalFormat  df ;
                float num;
                header.add(one.get(i).getTime());//表头
                addnum.add(one.get(i).getNumber()+"");//新增数
                winnum.add(two.get(i).getNumber()+"");//赢单率
                addCount += one.get(i).getNumber();//合计新增数
                winCount +=two.get(i).getNumber();//合计赢单数
                if(one.get(i).getNumber()==0 || two.get(i).getNumber()==0){
                    num = 0;
                }else{
                    num = (float) two.get(i).getNumber()*100/one.get(i).getNumber();
                }
                df =new DecimalFormat("0.00");
                String s=df.format(num)+"%";
                winprobability.add(s);
            }
            DecimalFormat  df1 ;
            float num1;
            if(addCount == 0 || winCount == 0){
                num1 =0;
            }else{
                num1 = (float)winCount*100/addCount;
            }
            df1 =new DecimalFormat("0.00");
            String s1 = df1.format(num1)+"%";
            header.add("合计");
            addnum.add(addCount+"");
            winnum.add(winCount+"");
            winprobability.add(s1);
            Map<String,Object> map = new HashMap<>();
            map.put("header",header);
            map.put("addnum",addnum);
            map.put("winnum",winnum);
            map.put("winprobability",winprobability);
            return map;
    }


    /**
     * 获取某年的月份
     * @param valueInfo
     * @return
     */
    public List<MonthInfo> getMonth(Integer valueInfo) {
        List<MonthInfo> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int weekYear = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        if(valueInfo==weekYear){
            for(int index=0;index<month;index++){
                MonthInfo info = new MonthInfo();
                info.setMonthId(index+1);
                info.setMonthValue(index+1+"月");
                list.add(info);
            }
        }
        if(valueInfo<weekYear){
            for(int index=0;index<12;index++){
                MonthInfo info = new MonthInfo();
                info.setMonthId(index+1);
                info.setMonthValue(index+1+"月");
                list.add(info);
            }
        }

        if(valueInfo>weekYear){
            return list;
        }
        return list;
    }




    /**
     * 获取某年某月的所有天数
     */
    public List<MonthInfo> getDay(Integer year,Integer month){
        Date date=new Date();
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date2=sdf.format(date);
        String str [] = new String[3];
        str = date2.split("-");
        Integer nowYear = Integer.parseInt(str[0]);//当前年
        Integer nowMonth = Integer.parseInt(str[1]);//当前月
        Integer nowDay = Integer.parseInt(str[2]);//当前天
        System.out.println("年:"+nowYear+"月："+nowMonth+"日："+nowDay);
        Calendar c= Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH,month-1);//注意一定要写5，不要写6！Calendar.MONTH是从0到11的！
        int n=c.getActualMaximum(Calendar.DAY_OF_MONTH);
        Integer days=0;//天数
        if(year.equals(nowYear) && month.equals(nowMonth)){//如果是当前年当前月天数为当天的天数
            days = nowDay;
        }else{
            days = n;
        }
        List<MonthInfo> list = new ArrayList<>();
        for(int i=0;i<days;i++){
            MonthInfo info = new MonthInfo();
            info.setMonthId(i+1);
            info.setMonthValue((i+1)+"日");
            list.add(info);
            System.out.println("天数:"+(i+1)+"日");
        }
        return list;
    }

}
